<template>
  <div class="search-history">
    <van-cell class="search-cell" :border="false" title="最近搜索">
      <div v-if="isDeleteShow">
        <span @click="$emit('clear-search-histories')">全部删除</span
        >&nbsp;&nbsp;&nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isDeleteShow = true" />
    </van-cell>
    <div class="box-text">
      <span
        v-for="(item, index) in searchHistories"
        :key="index"
        @click="onSearchItemClick(item, index)"
        >{{ item }}</span
      >
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDeleteShow: false
    }
  },
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  created() {},
  mounted() {},
  methods: {
    onSearchItemClick(item, index) {
      if (this.isDeleteShow) {
        this.searchHistories.splice(index, 1)
      } else {
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style scoped lang="less">
.search-history {
  .search-cell {
    font-size: 26px;
    font-weight: bold;
  }
  .box-text {
    padding: 0 28px;
    span {
      color: #717171;
      background-color: #f5f5f5;
      font-size: 24px;
      display: inline-block;
      padding: 12px 16px;
      border-radius: 25px;
      margin: 12px;
    }
  }
}
</style>
